home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Confusion as to the proper use of MODULAS
- Date: 15 Feb 1996 06:59:41 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4fvhotINN7fv@keats.ugrad.cs.ubc.ca>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fthsu$1kl@ixnews7.ix.netcom.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4fthsu$1kl@ixnews7.ix.netcom.com>, KPN <wzjn@ix.netcom.com> wrote:
- >Confusion as to the proper use of MODULAS
- >
- >Very confused. Just as I think IÆve got it correct, I make-up a new
- >test, the results of which leave me mixed-up again.
- >
- >To begin with, this is the first rule I found out:
- >
- >Modulas operator simply shows: *** AFTER *** you divide, what is the
- >remainder?
- >EXAMPLE of this:
- > 9 % 7 = ?
- > 9 divided by 7 yields a remainder of 2
- > 9 % 7 = 2
- >
- >1) MODULAS means divide a number and give me the left over number. OK -
- >not bad.
- >
- >Next rule:
- >If the number to be divided is smaller than the number to divide by,
- >the result will always be the number being divided
- >EXAMPLE of this:
- >
- > 9 % 12 = ?
- > can not divide 9 by a larger number
- > 9 % 12 = 9
- >Now I have rule #2
- >
- >2) If the number to be divided is smaller than the nuber to be used as
- >a divisor, the result is the original number. OK - again, not bad.
-
- Umm.... these are not two rules but one! 9 div 12 = 0 , remainder 9.
-
- Btw, it's "modulus", not "modulas".
-
- >Here, begin my troubles: using MODULAS in an IF statement.
- >
- >I made up a test to see if a number I inputted was a 7. Sample code:
- >
- > if (number % 7)
- > printf("Not a 7\n");
- > else
- > printf("First integer was a 7\n");
- >
- >So, if the number WAS a 7, the ELSE would take over. OK. But, when I
- >enter in a ZERO, it still tells me that the number was a seven?
-
- You mean, if the number was "congruent to 0 (modulo 7)". You are not testing
- for equality with 7, but for divisibility by 7.
-
- The number zero is congruent to 0 (mod 7), so when you put in a zero it tells
- you that it is congruent, as is 7, 14, 21, 28, etc.
-
- Note that the % operator only gives you the smallest positive residue
- (i.e. remainder) if both operands are unsigned quantities. It does not
- necessarily give a positive remainder for negative numbers.
-
- >Am I going in the right direction? Can someone tell me what IÆm doing
- >incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
- >in an IF statement?
-
- Ummm... ever hear of the == operator? As in, if (number == 7) ...
-
- --
-
-